// uni-app请求封装
// import $config from '@/common/config'
// // #ifdef H5
// const BaseHost = '/api'
// // #endif
// console.log('代理url',BaseHost)
import { getCurrentFormattedTimeNum,suijione } from "../utils/check.js"
import {
	store
} from '@/store'
import { JSEncrypt } from 'jsencrypt'
import {
	getFormData
} from '@/utils/check.js'
import md5 from 'js-md5'
// import qs from 'qs'
let temp_request = []
let is_freshing = false
import i18n from '@/locale/index'
import base from '../utils/base.js'  //引入

let base1 = new base();

// export const socketUrl = 'ws://192.168.0.13:8085/flow-chain/notice/' //东哥的本地
// export const socketUrl = 'ws://hzapp.flowark.co/flow-chain/notice/'// 测试
export const socketUrl = 'ws://app.flowark.biz/flow-chain/notice/'// 线上
// export const socketUrl = 'ws://hzapp3.flowark.co/flow-chain/notice/'// 预线上

const BaseHost = 'http://app.flowark.biz/flow-chain/'  //线上正式
// const BaseHost =  'http://192.168.0.2:8085/flow-chain/'  //冰冰的本地
// const BaseHost =  'http://192.168.0.13:8085/flow-chain/' //东哥的本地
// const BaseHost =  'http://hzapp.flowark.co/flow-chain/' //测试
// const BaseHost =  'http://hzapp3.flowark.co/flow-chain/' //预上线
// const BaseHost = '/api'   
/**
 * @param {string} urlco
 * @param {string} method
 * @param {Object} data
 */
const http = function(method, url, data) {
	
	// 新建一个JSEncrypt对象
	let encryptor = new JSEncrypt()
	// 设置公钥 （o这是后端直接给我的）测试公钥
	// let publicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt5ZKZ8tr3tdC7Eh5KE1yU922xRO2HZNvLqJBwnXqTwoYqeOw5JsNVwNY/2VFkHyR+wWqiAnRNxPGwwx0wiSrVIO6ae/Yo8l5Li9fukEwQ6Se079AJlLY9CZucIkny2Nus0Bi4sDoLDg4KJ0QFXkSR5MyJvla69cpL93IMZ1JSPp/jgIvxEe7lFWRpqi/r2Icx/RCjXFOIBTNcLqAxzVUaGeFtNa7HjeUWXbdgtnvgsDcwd7onLJAmtdCD/0bDbmhb4nwHEg7ljr/ruue2Ou8gg1ujsxJjfHLVE4+knMwTaF6Ph3DWtpSjfoSKr7BRoHdC0v2GxLAALYF+kUp6qWSywIDAQAB'
	//公钥 正式
	let publicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArNEotiN/CkH6+F3ZSP2NpHb0AXJGBRSHYPY0eT+mRdbtrYOjkBKDgrjhMjUp9ZZci3KA53HKvv1WdPm40vvZvMzd4YTDW7pyiFN/+fiXHqU/Pjo/JIso8VxDMNOPOrTk0V5q6J6uZ7ZCllFibhuYxX0uFbRy5E2smdfEJVKxUFoCUhxmcPvWo8/KVl7+QBYesJfNTCWE9iqq1Yj8+td3dUDFWDiiKjkv/TOPiT9nK2lBwXyYqoKwkcI3XKuLLjLmgsU7Tbku81wpmgRxhMNiH+cSXpQj6weWIMYwFrHqDVa4nKrg4tfh2R0W11TSdbY7fCtLLzTQtd6Qi8ke+w/spQIDAQAB'
	encryptor.setPublicKey(publicKey) // publicKey为公钥
 
	let string = JSON.stringify(data)
    let base64Str = base1.encode(string)

	let date =  getCurrentFormattedTimeNum()
 
	let nostr = suijione(true,true,false,16)
  
	let md5string = md5(url+base64Str+date+nostr)
	var jsonObj = {'secretData':encryptor.encrypt(string)} 
	let rsa = {
		'secretData':jsonObj.secretData,
		'md5Data':md5string,
		// 'base64Data':base64Str,
		'url':url,
		'date':date,
		'nostr':nostr
	}
	// console.log('wowowowowow',JSON.stringify(data))
	// console.log('加密：',encryptor.encrypt(JSON.stringify(data)))
	// console.log('加密明文：',data)
	return new Promise((resolve, reject) => {
		uni.request({
			url: BaseHost + url, //东哥的本地
			data:rsa,
			method: method || 'POST',
			header: {
				'Authorization': uni.getStorageSync('setToken') || ''
			},
			success(response) {
				const {
					success,
					errorCode,
					model,
					msg: title

				} = response.data
				if (success) {
					resolve(model)
					encryptor.decrypt(resolve(model));
				} else if (errorCode == '10001') {
					uni.showToast({
						title: `${i18n.global.t('身份過期重新登陸')}`,
						icon: 'none',
					})
					uni.hideLoading()
					uni.reLaunch({
						url: '/pages/login/login'
					});
					
				}else if(errorCode == '1039'){
					
				} else {
					const {
						success,
						errorCode,
						model,
						msg: title
					} = response.data
					console.log('err', title);
					uni.showToast({
						title: i18n.global.t(`${title}`),
						icon: 'none',
					})
				}
			},
			fail(err) {
				uni.showToast({
					title: `${i18n.global.t('請求失敗')}`,
					icon: 'none',
				})
				uni.hideLoading()
				reject(err);

			}
		})
	})
}
const httpLogin = function(method, url, data) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: BaseHost + url, //东哥的本地
			data,
			method: method || 'POST',
			header: {
				'Authorization': uni.getStorageSync('setToken') || ''
			},
			success(response) {
				const {
					success,
					errorCode,
					model,
					msg: title

				} = response.data
				if (success) {
					resolve(model);
				} else if (errorCode == '10001') {
					uni.showToast({
						title: `${i18n.global.t('身份過期重新登陸')}`,
						icon: 'none',
					})
					uni.hideLoading()
					uni.reLaunch({
						url: '/pages/login/login'
					});

				}else if(errorCode == '1039'){
					
				} else {
					const {
						success,
						errorCode,
						model,
						msg: title
					} = response.data
					console.log('err', title);
					uni.showToast({
						title: i18n.global.t(`${title}`),
						icon: 'none',
					})
				}
			},
			fail(err) {
				uni.showToast({
					title: `${i18n.global.t('請求失敗')}`,
					icon: 'none',
				})
				uni.hideLoading()
				reject(err);

			}
		})
	})
}
/**
 * @param {String} method 请求方式
 * @param {String} url 请求地址
 * @param {Object} data 请求参数
 * @description 请求方法
 */
export const axios = (url, data = {}, method = 'POST') => {
	return http(method, url, data)
}
export const axiosLogin = (url, data = {}, method = 'POST') => {
	return httpLogin(method, url, data)
}